from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
# 关于页面操作的封装
# 回顾面向对象：定义类，类中定义了属性和方法，通过类实例化对象，对象中包含了属性和方法
class PageLogin:
    # 初始化的操作，比如属性添加，都是写在构造方法中
    def __init__(self):
        # 获取浏览器驱动
        self.driver = webdriver.Firefox()
        self.driver.maximize_window()
        self.driver.implicitly_wait(20)
        self.driver.get('http://192.168.88.100:8000/admin')
    # 定位账号并输入
    def page_input_username(self, username):
        ele = self.driver.find_element(By.CSS_SELECTOR, '#id_username')
        # 清空内容
        ele.clear()
        ele.send_keys(username)
    # 定位密码并输入
    def page_input_password(self, password):
        self.driver.find_element(By.CSS_SELECTOR, '#id_password').send_keys(password)
    # 点击登录按钮
    def page_click_login_btn(self):
        self.driver.find_element(By.CSS_SELECTOR, '[value="登录"]').click()
    # 组装登录方法
    def page_login(self, username, password):
        self.page_input_username(username)
        self.page_input_password(password)
        self.page_click_login_btn()
    # 获取登录信息
    def page_get_login_info(self):
        return self.driver.find_element(By.CSS_SELECTOR, '.errornote').text
    # 关闭浏览器
    def page_close(self):
        sleep(3)
        self.driver.quit()
if __name__ == '__main__':
    # 通过类实例化对象
    login = PageLogin()
    # 调用登录方法
    login.page_login('admin', '888111')
    msg = login.page_get_login_info()
    print('登录结果：', msg)
    login.page_close()